package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/8

*/
public class WebTheadfangtianxia extends Thread {
    String string;
    Document doc;


    WebTheadfangtianxia(String str, Document docc){
        string= str;
        doc= docc;
    }
    private static Logger logger = Logger.getLogger(WebTheadfangtianxia.class);

    public static void main(String[] args) throws IOException {
        String proxyHost = "127.0.0.1";
        String proxyPort = "11478";
        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);
        // 对https也开启代理
        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);  //设置代理来访问豆瓣

        String link = "https://nj.5i5j.com/zufang/n2/";
        Connection connect = Jsoup.connect(link);
        Document doc = connect.get();
        Elements elements = doc.getElementsByClass("pList");
        for (Element element : elements) {
            System.out.println(element.getElementsByTag("li"));
        }

    }

    synchronized public void run() {
        try {
            Elements listCon = doc.getElementsByClass("listing-ad  item-regular ");

        for (Element element : listCon) {
            Thread.sleep(2000);
            //获取房子的图片链接
            String imgUrl = element.child(0).getElementsByTag("img").attr("data-originSource");
            //获取房子的具体信息链接
            String houseSingleUrl = element.child(0).getElementsByTag("a").attr("href");
           //房子的标题
            String title = element.getElementsByClass("ad-title").html();

            String replace = element.getElementsByClass("ad-item-detail").html().split("\n")[0];
            String[] info = replace.split(" / ");
            //获取房型：1室2厅
            String singleType = "";
            //获取面积
            String size = "";
            //获取精装，简装
            String direcType = "";
            //获取楼层
            String singleLayer = "";
            //整租，合租，出租类型
            String rentType = "";
            //获取房型
            singleType = info[1];
            //获取面积
            size = info[2];
            //获取楼层
            direcType = info[3];
            //获取出租类型
            singleLayer = info[5];
            rentType = info[0];

            //秦淮-月牙湖-天坛新村^空房
            Thread.sleep(2000);
            String place = element.getElementsByClass("ad-item-detail").html().split("\n")[1].replace("&nbsp;","");
            //南京市月牙湖小学
            Elements elementsByClass = element.getElementsByClass("ad-item-fang-tags");
            String underPlace = "";
            if (elementsByClass.toString() == null || "".equals(elementsByClass.toString())){
                underPlace = "暂无信息";
            }else{
                underPlace = elementsByClass.get(0).child(0).child(1).html();
            }
            //房子的价格
            String price = element.getElementsByClass("highlight").html().replace("元","");
            //输出信息到日志
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(imgUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(houseSingleUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(title.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(size.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(direcType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleLayer.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(rentType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(place.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(underPlace.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(price.replace("\t","").replace("\r","").replace("\n",""));
            logger.info(stringBuilder.toString());
        }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }



    }
}
